// CLoginPage.cpp : implementation file
//

#include "stdafx.h"
#include "Resource.h"
#include "LoginPage.h"

// CLoginPage dialog
CLoginPage::CLoginPage()
{
		m_pWindow = NULL;

		m_btn2 = NULL;
		m_btn1 = NULL;
		m_btn3 = NULL;
		m_btn0 = NULL;
		m_btnDel = NULL;
		m_btn6 = NULL;
		m_btn5 = NULL;
		m_btn4 = NULL;
		m_btn7 = NULL;
		m_btn8 = NULL;
		m_btn9 = NULL;
		m_btnClear = NULL;
		m_editUser = NULL;
		m_editPsw = NULL;
		m_btnLogin = NULL;
}

CLoginPage::~CLoginPage()
{
}

BEGIN_UIG_MESSAGE_MAP(CLoginPage)

	ON_UIG_BN_CLICKED(m_btn2, BtnNumber)
	ON_UIG_BN_CLICKED(m_btn1, BtnNumber)
	ON_UIG_BN_CLICKED(m_btn3, BtnNumber)
	ON_UIG_BN_CLICKED(m_btn0, BtnNumber)
	ON_UIG_BN_CLICKED(m_btnDel, BtnNumber)
	ON_UIG_BN_CLICKED(m_btn6, BtnNumber)
	ON_UIG_BN_CLICKED(m_btn5, BtnNumber)
	ON_UIG_BN_CLICKED(m_btn4, BtnNumber)
	ON_UIG_BN_CLICKED(m_btn7, BtnNumber)
	ON_UIG_BN_CLICKED(m_btn8, BtnNumber)
	ON_UIG_BN_CLICKED(m_btn9, BtnNumber)
	ON_UIG_BN_CLICKED(m_btnClear, BtnNumber)
	ON_UIG_BN_CLICKED(m_btnLogin, BtnClicked)
END_UIG_MESSAGE_MAP()

// CLoginPage message handlers
LRESULT CLoginPage::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	m_pWindow = UIGears::CreateUIGearsWinByName(_T("LoginPage.json"), (void*)m_hWnd);
	m_btn2 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn2"));
	m_btn1 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn1"));
	m_btn3 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn3"));
	m_btn0 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn0"));
	m_btnDel = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btnDel"));
	m_btn6 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn6"));
	m_btn5 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn5"));
	m_btn4 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn4"));
	m_btn7 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn7"));
	m_btn8 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn8"));
	m_btn9 = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btn9"));
	m_btnClear = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btnClear"));
	m_editUser = (UIGears::IUIGEdit*)m_pWindow->GetControl(_T("editUser"));
	m_editPsw = (UIGears::IUIGEdit*)m_pWindow->GetControl(_T("editPsw"));
	m_btnLogin = (UIGears::IUIGButton*)m_pWindow->GetControl(_T("btnLogin"));

	CenterWindow() ;

	InitMessageMap();
	return 0;
}


void CLoginPage::BtnNumber(IUIGearsControl* pCtrl)
{
	if(pCtrl == m_btn2)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("2");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btn1)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("1");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btn3)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("3");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btn0)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("0");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btnDel)
	{
		std::tstring text = m_editUser->GetText();
		if (text.size())
		{
			text.erase(text.begin() + text.size() - 1);
			m_editUser->SetText(text.c_str());
		}
	}
	else if(pCtrl == m_btn6)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("6");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btn5)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("5");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btn4)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("4");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btn7)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("7");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btn8)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("8");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btn9)
	{
		std::tstring text = m_editUser->GetText();
		text += _T("9");
		m_editUser->SetText(text.c_str());
	}
	else if(pCtrl == m_btnClear)
	{
		m_editUser->SetText(_T(""));
	}
}

void CLoginPage::BtnClicked(IUIGearsControl* pCtrl)
{
	if(pCtrl == m_btnLogin)
	{
		EndDialog(0);
	}
}

